package com.bootx.util;

import com.bootx.entity.Article;
import com.bootx.entity.Product;
import com.bootx.entity.ProductCategory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author black
 */
public class AiBotUtils {

    public static Map<String, ProductCategory> getCategory(){
        Map<String, ProductCategory> map = new HashMap<>();
        String s = WebUtils.get("https://ai-bot.cn/");
        System.out.println(s);
        Document parse = Jsoup.parse(s);
        Element elementsByClass = parse.getElementsByClass("sidebar-menu-inner").first();
        if(elementsByClass!=null){

            elementsByClass.getElementsByTag("li").forEach(element -> {
                Element a = element.getElementsByTag("a").first();
                if(a==null){
                    return;
                }
                String href = a.attr("href").replace("#term-","");
                System.out.println(href);
                String[] split = href.split("-");
                if(split.length==1){
                    map.put(split[0], new ProductCategory(split[0], a.text()));
                }else{
                    ProductCategory parent = map.get(split[0]);
                    if(parent==null){
                        map.put(split[0], new ProductCategory(split[0], a.text()));
                    }
                    parent = map.get(split[0]);
                    parent.getChildren().add(new ProductCategory(split[1], a.text()));
                }
            });
            System.out.println(map);
        }
        return map;
    }


    public static List<Product> getItem(String otherId){
        List<Product> products = new ArrayList<>();
        String s = WebUtils.get("https://ai-bot.cn/wp-admin/admin-ajax.php?id=" + otherId + "&taxonomy=favorites&action=load_home_tab");
        Document parse = Jsoup.parse(s);
        parse.getElementsByTag("a").forEach(element -> {
            Element a = element.getElementsByTag("a").first();
            if(a==null){
                return;
            }
            Product product = new Product();
            product.setOtherUrl(a.attr("href"));
            product.setMemo(a.attr("title"));
            a.getElementsByTag("img").forEach(element1 -> {
                product.setLogo(element1.attr("data-src"));
            });
            a.getElementsByTag("strong").forEach(element1 -> {
                product.setName(element1.text());
            });
            String overflowClip1 = a.getElementsByClass("overflowClip_1").text();
            product.setMemo(overflowClip1);
            products.add(product);
        });
        return products;
    }
    public static Map<String,List<Article>> getNews(){
        Map<String,List<Article>> map = new HashMap<>();
        String s = WebUtils.get("https://ai-bot.cn/daily-ai-news/");
        System.out.println(s);
        Element first = Jsoup.parse(s).getElementsByClass("news-list").first();
        Elements elementsByClass = first.getElementsByClass("news-date");
        elementsByClass.forEach(element -> {
            map.put(element.text(),new ArrayList<>());
            Element element1 = element.nextElementSibling();
            while (element1!=null && element1.hasClass("news-item")){
                String href = element1.getElementsByTag("a").first().attr("href");
                String title = element1.getElementsByTag("a").first().text();
                Element first1 = element1.getElementsByTag("p").first();
                Element first2 = first1.getElementsByTag("span").first();
                String source = first2.text();
                first2.remove();
                String memo = first1.html();
                Article article = new Article();
                article.setMemo(memo);
                article.setTitle(title);
                article.setSource(source);
                article.setUrl(href);
                map.get(element.text()).add(article);
                element1 = element1.nextElementSibling();
            }
        });
        return map;
    }

    public static List<Article> getNews1(){
        List<Article> articles = new ArrayList<>();
        for (int i = 1; i < 20; i++) {
            String s = WebUtils.get("https://ai-bot.cn/ai-hall-of-fame/page/"+i+"/");
            Element first = Jsoup.parse(s).getElementsByClass("cat_list").first();
            if(first==null){
                continue;
            }
            Elements elementsByClass = first.getElementsByClass("list-item");
            elementsByClass.forEach(element -> {
                String overflowClip2 = element.getElementsByClass("overflowClip_2 ").html();
                Element first1 = element.getElementsByTag("a").first();
                Article article = new Article();
                article.setTitle(first1.attr("title"));
                article.setMemo(overflowClip2);
                article.setUrl(first1.attr("href"));
                article.setCover(first1.attr("data-src"));
                articles.add(article);
            });


        }
        return articles;
    }

    public static Article getArticleDetail(String url){
        Article article = new Article();

        String s = WebUtils.get(url);
        Element first = Jsoup.parse(s);
        String title = first.getElementsByTag("title").text();

        Elements meta = first.getElementsByTag("meta");
        meta.forEach(element -> {
            String attr = element.attr("property");
            String value = element.attr("content");
            if("og:description".equals(attr)){
                article.setMemo(value);
            }else if("article:published_time".equals(attr)){
                article.setPublishDate(DateUtils.formatStringToDate(value.replace("T"," ").replace("+08:00",""),"yyyy-MM-dd HH:mm:ss"));
            }
        });

        Element first1 = first.getElementsByClass("panel-body").first();
        if(first1!=null){
            article.setContent(first1.html());
        }else{
            System.out.println(url);
        }

        return article;
    }

    public static Product getProductDetail(String url){
        Product product = new Product();

        String s = WebUtils.get(url);
        Element first = Jsoup.parse(s);
        System.out.println(url);
        try {
            String image = first.getElementsByClass("siteico").getFirst().getElementsByTag("img").attr("data-src");
            product.setImage(image);
            String title = first.getElementsByTag("title").text().replace(" | AI工具集","");
            product.setName(title);
            Elements meta = first.getElementsByTag("meta");
            meta.forEach(element -> {
                String attr = element.attr("property");
                String value = element.attr("content").replace(" | AI工具集","");
                if("og:description".equals(attr)){
                    product.setSeoDescription(value);
                }else if("article:published_time".equals(attr)){
                    product.setPublishDate(DateUtils.formatStringToDate(value.replace("T"," ").replace("+08:00",""),"yyyy-MM-dd HH:mm:ss"));
                }else if("og:image".equals(attr)){
                    product.setImage(value);
                }else if("og:title".equals(attr)){
                    product.setSeoTitle(value);
                }else if("description".equals(attr)){
                    product.setMemo(value);
                    product.setDescription(value);
                }


            });

            Elements link = first.getElementsByTag("link");
            link.forEach(element -> {
                String rel = element.attr("rel");
                String href = element.attr("href");
                if("shortcut icon".equals(rel)){
                    product.setIcon(href);
                    product.setLogo(href);
                }
            });
            Element panel = first.getElementsByClass("panel-body").first();
            if(panel!=null){
                product.setContent(panel.html());
            }
            return product;
        }catch (Exception e){
            return null;
        }
    }



    public static void main(String[] args) {
        Product product = getProductDetail("https://ai-bot.cn/sites/17330.html");
        System.out.println(product);
    }
}